package turtle.system.pt;
import java.io.*;
import java.util.*;
public class ParaTurner {
	public static int HOPE_PROFIT = 6000;
	public static int STOP_STEP = 3;
	public static int DAY_STEP = 2;
	public static int ITERATION = 2;

	private static int[][] inAndOut_CONSERVATIVE = new int[][] { // 特別觀察組合
	        {3,16,15,22}  //保守穩健系統				  
	};
	private static int[][] inAndOut_POSITIVE = new int[][] { // 特別觀察組合
	   {9,21,3,5}  //積極進取系統		  
	};
	private static int[] LOSS_STOP_RANGE_FOR_SHORT_CONSERVATIVE = { 31, 31 }; // 做空停損點數實驗區間	  
	private static int[] LOSS_STOP_RANGE_FOR_SHORT_POSITIVE = { 87, 87 }; // 做空停損點數實驗區間

	private static int[] LOSS_STOP_RANGE_FOR_LONG_CONSERVATIVE = { 10, 10 }; // 做多停損點數實驗區間	  
	private static int[] LOSS_STOP_RANGE_FOR_LONG_POSITIVE = { 31, 31 }; // 做多停損點數實驗區間	  
	  
	  
	  public void changeArray(int[] array, int value) {
		  for (int i=0;i<array.length;i++) {
			  array[i]+=i;
		  }
	  }
	  public void changeValue(int original, int value) {
		  original+=value;
	  }
	  public String getPara() {
		  StringBuffer buf = new StringBuffer();
		  buf.append("["+inAndOut_CONSERVATIVE[0][0]+"-"+inAndOut_CONSERVATIVE[0][1]+
				                inAndOut_CONSERVATIVE[0][2]+"-"+inAndOut_CONSERVATIVE[0][3]+"]");
		  buf.append("["+inAndOut_POSITIVE[0][0]+"-"+inAndOut_POSITIVE[0][1]+
				  inAndOut_POSITIVE[0][2]+"-"+inAndOut_POSITIVE[0][3]+"]");
          buf.append("["+LOSS_STOP_RANGE_FOR_SHORT_CONSERVATIVE[0]+"-"+LOSS_STOP_RANGE_FOR_SHORT_CONSERVATIVE[1]+"]");
          buf.append("["+LOSS_STOP_RANGE_FOR_SHORT_POSITIVE[0]+"-"+LOSS_STOP_RANGE_FOR_SHORT_POSITIVE[1]+"]");
          buf.append("["+LOSS_STOP_RANGE_FOR_LONG_CONSERVATIVE[0]+"-"+LOSS_STOP_RANGE_FOR_LONG_CONSERVATIVE[1]+"]");
          buf.append("["+LOSS_STOP_RANGE_FOR_LONG_POSITIVE[0]+"-"+LOSS_STOP_RANGE_FOR_LONG_POSITIVE[1]+"]");
          return buf.toString();
	  }
	  public ParaTurner() throws IOException {
		BufferedWriter writer = new BufferedWriter(new FileWriter("tmp/para.csv"));
		for (int i1=0;i1<ITERATION;i1++) {
			changeArray(LOSS_STOP_RANGE_FOR_SHORT_CONSERVATIVE, STOP_STEP);
			changeArray(LOSS_STOP_RANGE_FOR_SHORT_POSITIVE, STOP_STEP);
			for (int i2=0;i2<ITERATION;i2++) {
				changeArray(LOSS_STOP_RANGE_FOR_LONG_CONSERVATIVE, STOP_STEP);
				changeArray(LOSS_STOP_RANGE_FOR_LONG_POSITIVE, STOP_STEP);
				for (int i3=0;i3<ITERATION;i3++) {
					changeValue(inAndOut_CONSERVATIVE[0][0], DAY_STEP);
					changeValue(inAndOut_POSITIVE[0][0], DAY_STEP);
					for (int i4=0;i4<ITERATION;i4++) {
						changeValue(inAndOut_CONSERVATIVE[0][1], DAY_STEP);
						changeValue(inAndOut_POSITIVE[0][1], DAY_STEP);
						for (int i5=0;i5<ITERATION;i5++) {
							changeValue(inAndOut_CONSERVATIVE[0][2], DAY_STEP);
							changeValue(inAndOut_POSITIVE[0][2], DAY_STEP);
							for (int i6=0;i6<ITERATION;i6++) {
								changeValue(inAndOut_CONSERVATIVE[0][3], DAY_STEP);
								changeValue(inAndOut_POSITIVE[0][3], DAY_STEP);
								PriceData.LOSS_STOP_RANGE_FOR_SHORT_CONSERVATIVE = LOSS_STOP_RANGE_FOR_SHORT_CONSERVATIVE;
								PriceData.LOSS_STOP_RANGE_FOR_SHORT_POSITIVE = LOSS_STOP_RANGE_FOR_SHORT_POSITIVE;
								PriceData.LOSS_STOP_RANGE_FOR_LONG_CONSERVATIVE = LOSS_STOP_RANGE_FOR_LONG_CONSERVATIVE;
								PriceData.LOSS_STOP_RANGE_FOR_LONG_POSITIVE = LOSS_STOP_RANGE_FOR_LONG_POSITIVE;
								PriceData.inAndOut_CONSERVATIVE = inAndOut_CONSERVATIVE;
								PriceData.inAndOut_POSITIVE = inAndOut_POSITIVE; 
								new YearEvaluator();
								double v = getDeviationWRTHope(this.readIntegHist());
								writer.write(this.getPara()+","+v+"\n");
							}
						}
						writer.flush();
					}
					writer.flush();
				}
				writer.flush();
			}			
			writer.flush();
		}
		writer.close();
	  }
	  public ParaTurner(boolean negative) throws IOException {
			BufferedWriter writer = new BufferedWriter(new FileWriter("tmp/para_neg.csv"));
			for (int i1=0;i1<ITERATION;i1++) {
				changeArray(LOSS_STOP_RANGE_FOR_SHORT_CONSERVATIVE, -STOP_STEP);
				changeArray(LOSS_STOP_RANGE_FOR_SHORT_POSITIVE, -STOP_STEP);
				for (int i2=0;i2<ITERATION;i2++) {
					changeArray(LOSS_STOP_RANGE_FOR_LONG_CONSERVATIVE, -STOP_STEP);
					changeArray(LOSS_STOP_RANGE_FOR_LONG_POSITIVE, -STOP_STEP);
					for (int i3=0;i3<ITERATION;i3++) {
						changeValue(inAndOut_CONSERVATIVE[0][0], -DAY_STEP);
						changeValue(inAndOut_POSITIVE[0][0], -DAY_STEP);
						for (int i4=0;i4<ITERATION;i4++) {
							changeValue(inAndOut_CONSERVATIVE[0][1], -DAY_STEP);
							changeValue(inAndOut_POSITIVE[0][1], -DAY_STEP);
							for (int i5=0;i5<ITERATION;i5++) {
								changeValue(inAndOut_CONSERVATIVE[0][2], -DAY_STEP);
								changeValue(inAndOut_POSITIVE[0][2], -DAY_STEP);
								for (int i6=0;i6<ITERATION;i6++) {
									changeValue(inAndOut_CONSERVATIVE[0][3], -DAY_STEP);
									changeValue(inAndOut_POSITIVE[0][3], -DAY_STEP);
									PriceData.LOSS_STOP_RANGE_FOR_SHORT_CONSERVATIVE = LOSS_STOP_RANGE_FOR_SHORT_CONSERVATIVE;
									PriceData.LOSS_STOP_RANGE_FOR_SHORT_POSITIVE = LOSS_STOP_RANGE_FOR_SHORT_POSITIVE;
									PriceData.LOSS_STOP_RANGE_FOR_LONG_CONSERVATIVE = LOSS_STOP_RANGE_FOR_LONG_CONSERVATIVE;
									PriceData.LOSS_STOP_RANGE_FOR_LONG_POSITIVE = LOSS_STOP_RANGE_FOR_LONG_POSITIVE;
									PriceData.inAndOut_CONSERVATIVE = inAndOut_CONSERVATIVE;
									PriceData.inAndOut_POSITIVE = inAndOut_POSITIVE; 
									new YearEvaluator();
									double v = getDeviationWRTHope(this.readIntegHist());
									writer.write(this.getPara()+","+v);
								}
							}
							writer.flush();
						}
						writer.flush();
					}
					writer.flush();
				}			
				writer.flush();
			}
			writer.close();
		  }	  
	  public double getDeviationWRTHope(StatCalc integ) {
		  ArrayList<Double> value = integ.valueList;
		  StatCalc tmp = new StatCalc();
		  for (int i=0;i<value.size();i++) {
			  tmp.enter(value.get(i).doubleValue()-HOPE_PROFIT);
		  }
		  return tmp.getStandardDeviation();
	  }
	  private StatCalc readIntegHist() {
		  StatCalc integ = new StatCalc();
		  try {
		    BufferedReader reader = new BufferedReader(new FileReader("tmp/integHist.csv"));
		    String line;
		    while ((line=reader.readLine())!=null) {
		    	integ.enter(Double.parseDouble(line));
		    }
		    reader.close();
		  } catch (Exception e) {
			  System.out.println(e);
		  }
		  return integ;
	  }
	  public static void main(String[] args){
		  if (args.length>=1) {
		    try {
			new ParaTurner();
		    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		  }
	    }
		  else {
			    try {
					new ParaTurner(true);
				    } catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				  }			  
		  }
	  }
}
